package com.cmpe.av.androidservice;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cmpe.av.logic.ReportLogic;
import com.cmpe.av.logic.UserLogic;
import com.cmpe.av.model.Report;
import com.cmpe.av.model.User;

/**
 * Servlet implementation class AndroidRate
 */
@WebServlet("/AndroidRate")
public class AndroidRate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AndroidRate() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try{
			ReportLogic msAccess = new ReportLogic();
			UserLogic msAccess2 = new UserLogic();
			long reportID = Long.parseLong(request.getParameter("reportID"));
			String operation = request.getParameter("operation");
			long userID = Long.parseLong(request.getParameter("userID"));
			Report report = msAccess.viewReport(reportID);
			int rate;
			int score;
			int wrongReport;
			int reportRate;
			if (operation.equals("up")) {

				int localUserID = -1;
				localUserID = msAccess.getReportOwner(reportID);
				User owner = new User();
				owner = msAccess2.getUser(localUserID);

				if (!(userID == localUserID)) {
					if (!(msAccess.IsReportVotedByUserIdAndReportId(userID,
							reportID))) {
						msAccess.insertReportRate(userID, reportID);
						rate = report.getRate() + 1;
						msAccess.updateRate(rate, reportID);

						score = owner.getScore();
						wrongReport = owner.getNo_wrong_reports();
						reportRate = owner.getSum_report_rates();

						// means correct report
						if (rate == 5) {
							score++;
						}
						msAccess2.updateUser(localUserID, score, wrongReport,
								reportRate + 1);
						response.getWriter().print("success");
					} else {
						response.getWriter().print("already");
					}
				} else {
					response.getWriter().print("own");
				}

				report = msAccess.viewReport(reportID);
			}else if (operation.equals("down")) {

				int localUserID = -1;
				localUserID = msAccess.getReportOwner(reportID);
				User owner = new User();
				owner = msAccess2.getUser(localUserID);

				if (!(userID==localUserID)) {
					if (!(msAccess.IsReportVotedByUserIdAndReportId(userID,
							reportID))) {
						msAccess.insertReportRate(userID, reportID);
						rate = report.getRate() - 1;
						msAccess.updateRate(rate, reportID);

						score = owner.getScore();
						wrongReport = owner.getNo_wrong_reports();
						reportRate = owner.getSum_report_rates();

						// means incorrect report
						if (rate == -5) {
							wrongReport++;
							score--;

							if (wrongReport == 5) {
								msAccess2.banUser(localUserID);
							} else {
								String mailBody = "Your report '"
										+ report.getReportName()
										+ "' does not reflect a real situation. If you keep writing wrong reports, you will be banned of the system."
										+ "We believe you will be more careful next time. Thank you for using our system.";
								msAccess.sendMail(owner.getEmail(),
										"Wrong Report!!", mailBody);
							}
						}

						msAccess2.updateUser(localUserID, score, wrongReport,
								reportRate - 1);
						response.getWriter().print("success");
					} else {
						response.getWriter().print("already");
					}
				} else {
					response.getWriter().print("own");
				}
			}
			
			msAccess.close();
			msAccess2.close();
		}catch(Exception e){
			response.getWriter().print("error");
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
